package dk.marten;

import java.util.Random;

import android.graphics.Color;
import android.util.Log;


public class Dot extends DotBasis{

    private float accFactor = 2;

    private float maxTouchDistance = 300;
    
    public float xAcc = 0;
    public float yAcc = 0;

    public float mouseX = 0;
    public float mouseY = 0;
    
    float dX;
    float dY;
    
    float dist;

    float effect;
    

    public boolean isMouseMove = false;
    
	float xSpeed = 1;
	float ySpeed = 1;
	int bredde = 100;
	int hojde = 100;
	Random myRandom;

	public Dot() {
		super();
		myRandom = new Random();
		radius = 5;

		paint.setColor(Color.argb(200, 250,250,250));
	}

	public void update() {
		if(isMouseMove) addTouch();
		xSpeed += (myRandom.nextFloat()-0.5)*accFactor + xAcc;
		ySpeed += (myRandom.nextFloat()-0.5)*accFactor + yAcc;		

		if((x+xSpeed)>bredde || (x+xSpeed)<radius) xSpeed = 0;
		if((y+ySpeed)>hojde || (y+ySpeed)<radius) ySpeed = 0;
		
		y += ySpeed;
		x += xSpeed;
				
	    if (xSpeed>5) xSpeed = 5;
	    if (ySpeed>5) ySpeed = 5;
	    if (xSpeed<-5) xSpeed = -5;
	    if (ySpeed<-5) ySpeed = -5;
	}
	
    private void addTouch() {
        dX = x-mouseX;
        dY = y-mouseY;
        
        dist = (float) Math.sqrt((dX * dX + dY * dY));

        if (dist >= maxTouchDistance) return;

        effect = ((maxTouchDistance - dist) / maxTouchDistance);
        xAcc -= effect * dX/dist;
        yAcc -= effect * dY/dist;        
    }

}
